//
//  ZYCTeaLeaveRecordFlowChartView.m
//  ZDSC
//
//  Created by 张威 on 2021/11/9.
//

#import "ZYCTeaLeaveRecordFlowChartView.h"

#import "ZYCTeaLeaveRecordDetailFlowChartCell.h"
#import "ZYCTeaLeaveFooterCollectionCell.h"
#import "ZYCTeaLeaveRecordFlowChartSectionHeaderView.h"
#import "UIView+Gt.h"

@interface ZYCTeaLeaveRecordFlowChartView ()<UITableViewDelegate,UITableViewDataSource,ZYCTeaLeaveFooterCollectionCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCTeaLeaveRecordFlowChartHeaderView *headerView;

@end
@implementation ZYCTeaLeaveRecordFlowChartView


#pragma mark - init方法
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        self.tableView.tableHeaderView = self.headerView;
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCTeaLeaveFlowChartModel *model = self.dataSource[indexPath.section];
    if ([model.applyUserList isKindOfClass:[NSArray class]]) {
        ZYCTeaLeaveFooterCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeaLeaveFooterCollectionCell class]) forIndexPath:indexPath];
        cell.model = model;
        cell.delegate = self;
        return cell;
    }else{
        ZYCTeaLeaveRecordDetailFlowChartCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeaLeaveRecordDetailFlowChartCell class]) forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZYCTeaLeaveFlowChartModel *model = self.dataSource[section];
    ZYCTeaLeaveRecordFlowChartSectionHeaderView *header  = [[ZYCTeaLeaveRecordFlowChartSectionHeaderView alloc] init];
    header.model = model;
    return header;
}

#pragma mark - ZYCTeaLeaveFooterCollectionCellDelegate
- (void)teaLeaveFooterCollectionCellDidAddApprover:(ZYCTeaLeaveFlowChartModel *)model{
    if ([self.delegate respondsToSelector:@selector(teaLeaveRecordFlowChartViewAddApprover:)]) {
        [self.delegate teaLeaveRecordFlowChartViewAddApprover:model];
    }
}

- (void)teaLeaveFooterCollectionCellDidDeleteApprover:(ZYCTeaLeaveApplyUserModel *)m{
    if ([self.delegate respondsToSelector:@selector(teaLeaveRecordFlowChartViewDeleteApprover:)]) {
        [self.delegate teaLeaveRecordFlowChartViewDeleteApprover:m];
    }
}

#pragma mark - private method
- (void)reload{
    [self.tableView reloadData];
}

#pragma mark - getters

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTeaLeaveRecordDetailFlowChartCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveRecordDetailFlowChartCell class])];
        [_tableView registerClass:[ZYCTeaLeaveFooterCollectionCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeaLeaveFooterCollectionCell class])];
    }
    return _tableView;
}

- (ZYCTeaLeaveRecordFlowChartHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCTeaLeaveRecordFlowChartHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.width, 50.0f)];
    }
    return _headerView;
}

#pragma mark - setters
- (void)setType:(ZYCFlowChartHeaderViewType)type{
    _type = type;
    self.headerView.type = type;
}
@end
